package com.woniuxy.microfixeddepositserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microfixeddepositserver.bean.FixedTimeDepositBean;
import com.woniuxy.microfixeddepositserver.bean.detail.RegularQuery;
import com.woniuxy.microfixeddepositserver.dao.FixedTimeDepositDao;
import com.woniuxy.microfixeddepositserver.service.IFixedDespositService;
import com.woniuxy.microfixeddepositserver.util.DateUtil;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: masterhai
 * @date: 2021/4/7:11:34
 * @description: 定期存款服务
 */
@Service
@Slf4j
public class FixedDespositServiceImpl implements IFixedDespositService {
    @Resource
    private FixedTimeDepositDao fixedTimeDepositDao;

    @Override
    public int deleteByPrimaryKey (List<String> ids) {
        return fixedTimeDepositDao.deleteByPrimaryKey(ids);
    }

    @Override
    public int insert (FixedTimeDepositBean fixedTimeDepositBean) {
        return fixedTimeDepositDao.insert(fixedTimeDepositBean);
    }

    @Override
    public FixedTimeDepositBean selectByPrimaryKey (Integer id, Integer status) {
        return fixedTimeDepositDao.selectByPrimaryKey(id, status);
    }

    @Override
    public PageInfo<FixedTimeDepositBean> selectAll (Integer page, Integer size, Integer status, Integer isHot) {
        PageHelper.startPage(page, size);
        List<FixedTimeDepositBean> depositBeans = null;
        if (isHot == 3) {
            depositBeans = fixedTimeDepositDao.selectAllNotHot(status);
        } else {
            depositBeans = fixedTimeDepositDao.selectAll(status, isHot);
        }
        for (int i = 0; i < depositBeans.size(); i++) {
            FixedTimeDepositBean depositBean = depositBeans.get(i);
            depositBean.setYieldRates((double)Math.round(depositBean.getYieldRates() * 10000) / 10000);
            depositBean.setPurchasingArray(depositBean.getPurchasing().split(","));
            depositBean.setIsHotName(depositBean.getIsHot() == 1 ? "明星产品" : "非明星产品");
        }
        return new PageInfo<FixedTimeDepositBean>(depositBeans);
    }

    @Override
    public int updateByPrimaryKey (FixedTimeDepositBean fixedTimeDepositBean) {
        return fixedTimeDepositDao.updateByPrimaryKey(fixedTimeDepositBean);
    }

    @Override
    public void recoverFixedDeposit (List<String> list) {
        fixedTimeDepositDao.recoverRegular(list);
    }

    @Override
    public PageInfo<FixedTimeDepositBean> selectAllByQuery (RegularQuery regularQuery) {
        PageHelper.startPage(regularQuery.getPage(), regularQuery.getSize());
        //针对名称字段拼接%
        String name = regularQuery.getName();
        if (! StringUtils.isEmpty(name)) {
            String newName = "%" + name + "%";
            regularQuery.setName(newName);
        }
        List<FixedTimeDepositBean> depositBeans = fixedTimeDepositDao.selectAllByQuery(regularQuery);
        return new PageInfo<FixedTimeDepositBean>(depositBeans);
    }

    @Override
    public int updateIsHot (RegularQuery regularQuery) {
        return fixedTimeDepositDao.updateIsHot(regularQuery.getFid(), regularQuery.getIsHot());
    }

    @Override
    public PageInfo<FixedTimeDepositBean> selectAllProduct (Integer page, Integer size, Integer status) {
        PageHelper.startPage(page, size);
        List<FixedTimeDepositBean> depositBeans = fixedTimeDepositDao.selectAllNotHot(status);

        ArrayList<FixedTimeDepositBean> finalBean = new ArrayList<>();
        //已卖完的产品集合
        ArrayList<FixedTimeDepositBean> sellOutBean = new ArrayList<>();
        //未到时间的产品集合
        ArrayList<FixedTimeDepositBean> notTimeBean = new ArrayList<>();
        sureSellTime(depositBeans, finalBean, notTimeBean, sellOutBean);
        for (FixedTimeDepositBean bean : notTimeBean) {
            finalBean.add(bean);
        }
        //遍历集合,将售完的产品放入最终集合
        for (FixedTimeDepositBean outBean : sellOutBean) {
            finalBean.add(outBean);
        }

        return new PageInfo<FixedTimeDepositBean>(finalBean);
    }

    private void sureSellTime (List<FixedTimeDepositBean> depositBeans, List<FixedTimeDepositBean> finalBean, ArrayList<FixedTimeDepositBean> notTimeBean, List<FixedTimeDepositBean> sellOutBean) {
        //获取当前时间
        Integer nowTime = Integer.valueOf(new DateTime().toString("yyyyMMddHH"));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH");
        //遍历集合,判断开始销售时间
        for (int i = 0; i < depositBeans.size(); i++) {
            FixedTimeDepositBean fOld = depositBeans.get(i);
            //格式化年利率
                fOld.setYieldRates((double) Math.round(fOld.getYieldRates() * 10000) / 10000);
            //分割购买需求
            fOld.setPurchasingArray(fOld.getPurchasing().split(","));
            //转换时间
            fOld.setPeriodTime(DateUtil.conversionDay(fOld.getPeriod()));
            String formatTime = format.format(fOld.getSellStartTime());
            Integer fTime = Integer.valueOf(formatTime.replaceAll("-", ""));
            if (nowTime - fTime < 0) {
                String[] split = formatTime.split("-");
                String[] sellTimeName = new String[2];
                String month = split[1];
                if (month.startsWith("0")) {
                    month = month.substring(month.indexOf("0") + 1);
                }
                String day = split[2];
                if (day.startsWith("0")) {
                    day = day.substring(day.indexOf("0") + 1);
                }
                String hour = split[3];
                if (hour.startsWith("0")) {
                    hour = hour.substring(month.indexOf("0") + 1);
                }
                sellTimeName[0] = month + "月" + day + "日";
                sellTimeName[1] = hour + "点开售";
                //未到销售时间总分数为零的为异常数据,自动补充100份额
                if (fOld.getSumCount() <= 0) {
                    fOld.setSumCount(100);
                }
                fOld.setSellTimeName(sellTimeName);
                //加入未开始销售集合
                notTimeBean.add(fOld);
            } else {
                //已经到销售时间的产品
                //售完了加到售完的集合
                if (fOld.getSumCount() <= 0) {
                    sellOutBean.add(fOld);
                } else {
                    finalBean.add(fOld);
                }

            }
        }
    }

}
